<?php

namespace app\controller;

use app\BaseController;
use \app\model\Oss;
use \app\model\Video;
use Qiniu\Auth;
use Qiniu\Config;
use Qiniu\Storage\UploadManager;
use Qiniu\Storage\BucketManager;
class Upload extends BaseController
{
    public $config;

    public function initialize()
    {

        $this->config = getConfig();
    }
    // http://www.penyou.com/upload/image
    public function image()
    {

        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('image');
        // 上传到本地服务器
        $savename = \think\facade\Filesystem::disk('public')->putFile('uploads/image', $file);

//        $result = "http://www.penyou.com/storage/" . $savename;
//        singleJson($result, '上传失败');
        $savename_replace = str_replace("\\", "/", $savename);
        $fileName = "storage/" . $savename_replace;

        $rs = $this->upload($fileName);
      //  print_r($rs);exit;
        $imagePath = $this->config['qiniu_domain'].$rs['file'];
        singleJson($imagePath);
    }

    public function imageEditor()
    {

        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('image');
        // 上传到本地服务器
        $savename = \think\facade\Filesystem::disk('public')->putFile('uploads/image', $file);

//        $result = "http://www.penyou.com/storage/" . $savename;
//        singleJson($result, '上传失败');
        $savename_replace = str_replace("\\", "/", $savename);
        $fileName = "storage/" . $savename_replace;

        $rs = $this->upload($fileName,'editor'.'/'.date('Y-m-d'));
        //  print_r($rs);exit;
        $imagePath = $this->config['qiniu_domain'].$rs['file'];
        singleJson($imagePath);
    }

    public function video()
    {

        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('video');
        // 上传到本地服务器
        $savename = \think\facade\Filesystem::disk('public')->putFile('uploads/video', $file);

        $savename_replace = str_replace("\\", "/", $savename);
        $fileName = "storage/" . $savename_replace;

        $rs = $this->upload($fileName);

        singleJson($rs);
    }

    public  function upload($file,$fileParent='temp')
    {
        if(!$file){
            singleJson(false,'没有文件file');
        }

        // 用于签名的公钥和私钥
        $accessKey = $this->config['qiniu_accessKey'];
        $secretKey = $this->config['qiniu_secretKey'];
        $bucket = $this->config['qiniu_bucket'];
        // 初始化签权对象
        $auth = new Auth($accessKey, $secretKey);

        // 生成上传Token
        $token = $auth->uploadToken($bucket);

        // 构建 UploadManager 对象
        $uploadMgr = new UploadManager();
        $fileName = basename($file);
        $key =$fileParent.'/'.$fileName;  //文件名

        list($ret, $err) = $uploadMgr->putFile($token, $key, $file);

        $arr = array(
            'error'=>$err,
            'file'=>$key
        );
        return $arr;
    }

    public  function  move($key,$id){

        if(!$key){
            singleJson(false,'没有文件file');
        }
        if(!$id){
            singleJson(false,'没有id');
        }
        $accessKey = $this->config['qiniu_accessKey'];
        $secretKey = $this->config['qiniu_secretKey'];
        $bucket = $this->config['qiniu_bucket'];

        $auth = new Auth($accessKey, $secretKey);
        $config = new Config();
        $bucketManager = new \Qiniu\Storage\BucketManager($auth, $config);

        $fileName = basename($key);
        $destKey ='material/'.$id.'/'.md5('jn@'.$id).'/'.$fileName;  //文件名
        $err = $bucketManager->move($bucket, $key, $bucket, $destKey, true);
//        print_r($err);exit;
    }



}
